package com.engine.query.cmd;

import com.engine.core.gen.Mysql;
import com.engine.core.gen.PagedOption;
import com.engine.core.interceptor.JdbcCommandContext;
import com.engine.core.interceptor.ResponseCommand;
import com.engine.core.util.QueryUtil;
import com.engine.core.util.Response;
import com.engine.core.util.Utils;
import com.engine.system.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryPageCommand extends ResponseCommand {

    public QueryPageCommand(User user, Map<String, Object> params) {
        super(user, params);
    }

    /**
     * @param commandContext
     * @return
     */
    @Override
    public Response execute(JdbcCommandContext commandContext) {

        Response returnResponse=Response.static_success("执行成功!");


        PagedOption pagedOption=commandContext.getPagedOption();
        pagedOption.setParamMode(PagedOption.PARAM_MODE_MAP);

        try {

            //-------------------------------------第一个阶段 先构造sql和查询参数--------------------------------------------------------
            QueryResourceCommand.Playload resourcePlayload=QueryResourceCommand.Playload.newInstance(user)
                     .fethchResult(false).bodyContent((String)params.get(QueryUtil.QUERY_CONTENT));
            Response queryResourceRes=resourcePlayload.execute(commandContext);
            String sql=(String)queryResourceRes.get(QueryUtil.QUERY_RESULT_SQL);
            Map<String,Object> queryParams=(Map<String,Object>)queryResourceRes.get(QueryUtil.QUERY_RESULT_PARAMS);


            //-------------------------------------第二个阶段 分页的两条语句--------------------------------------------------------
            Map<String,Object> listQueryParams=new HashMap<>(queryParams);
            int current=Utils.getInt(params.get(QueryUtil.PAGED_CURRENT),1);
            int pageSize=Utils.getInt(params.get(QueryUtil.PAGED_PAGE_SIZE),10);
            listQueryParams.put(QueryUtil.PAGED_OFFSET,(current-1)*pageSize);
            listQueryParams.put(QueryUtil.PAGED_LIMIT,pageSize);


            String pageCountSql=Mysql.getPageCountSql(sql,pagedOption);
            QueryListCommand.Playload playload=QueryListCommand.Playload.newInstance(user).sql(pageCountSql)
                    .isMapParam(true).isConvertParam(false)
                    .addParams(queryParams);

            returnResponse=playload.execute(commandContext);
            returnResponse.set(QueryUtil.QUERY_RESULT_SQL,sql);

            if(returnResponse.isSuccess()){
                List<Map<String,Object>> countList=(List<Map<String,Object>>)returnResponse.get(QueryUtil.QUERY_RESULT);
                if(countList.size()>0){
                    Map<String,Object> countMap=countList.get(0);
                    returnResponse.set(QueryUtil.PAGED_PAGE_TOTAL,Utils.getInt(countMap.get("cn"),0));
                    int count=(int)returnResponse.get(QueryUtil.PAGED_PAGE_TOTAL);
                    listQueryParams.put(QueryUtil.PAGED_LIMIT,count>=(current*pageSize)?(current*pageSize):count-((current-1)*pageSize));
                    if(Utils.getInt(countMap.get("cn"),0)>0){
                        String pageSql=Mysql.getPageSql(sql,pagedOption);
                        QueryListCommand.Playload playloadList=QueryListCommand.Playload.newInstance(user).sql(pageSql)
                                .isMapParam(true).isConvertParam(false)
                                .addParams(listQueryParams);
                        Response listRes=playloadList.execute(commandContext);


                        AssociationCommand associationCommand=new AssociationCommand(user,params);
                        listRes=associationCommand.execute(listRes,commandContext);

                        returnResponse.setSuccess(listRes.isSuccess());
                        returnResponse.data(listRes.data());
                        returnResponse.setErr(listRes.getErr());
                    }else{
                        returnResponse.data(null);
                    }
                }else{
                    returnResponse.data(null);
                }
            }
        } catch (Exception e) {
            returnResponse.setErr(e);
        }
        return returnResponse;
    }
}
